package uni.androidgame.gameobjects;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;

import java.lang.*;
import java.util.ArrayList;
import java.util.HashMap;

import uni.androidgame.*;
import uni.androidgame.mapgeneration.Corridor;
import uni.androidgame.mapgeneration.Room;

public class Tile
{
    public enum Types{Wall,Floor,Door,Empty}

    private Types _type;
    public Types getType(){return _type;}
    private HashMap<Types,Integer> textureMap = new HashMap<Types, Integer>(){{
        put(Types.Wall, Color.rgb(51,25,0));
        put(Types.Floor,Color.rgb(120,120,120));
        put(Types.Door,Color.rgb(153,70,0));
        put(Types.Empty,Color.BLACK);
    }};

    public Boolean IsSolid(){return _type == Types.Wall;}

    private int _x;
    private int _y;
    public int getX(){return _x;}
    public int getY(){return _y;}
    private Point _tilePosition;
    public Point getTilePosition(){return _tilePosition;}
    private Rect _drawRect;

    private ArrayList<Tile> neighbours = new ArrayList<Tile>();
    public ArrayList<Tile> getNeighbours(){return neighbours;}
    private uni.androidgame.Character _character;
    public void setCharacter(uni.androidgame.Character character)
    {
        if(_character != null)
        {
            Log.w("Tile", "Tile already contains character!");
        }
        _character = character;
    }
    public void removeCharacter(){_character = null;}
    public uni.androidgame.Character getCharacter(){return _character;}
    public boolean isEmpty(){return _character == null;}
    private Room _roomOwner;
    public Room getRoomOwner(){return _roomOwner;}
    private Corridor _corridorOwner;
    public Corridor getCorridorOwner(){return _corridorOwner;}

    private Paint _paint;
    public Tile(Types type,int x,int y)
    {
        _type = type;
        _tilePosition = new Point(x,y);
        _x=x;
        _y=y;
        _drawRect = new Rect(DungeonMap.TileSize*x,DungeonMap.TileSize*y,DungeonMap.TileSize*x+DungeonMap.TileSize,DungeonMap.TileSize*y+DungeonMap.TileSize);
        _paint = new Paint();
        _paint.setColor(textureMap.get(_type));
    }

    public Tile(Types type,int x, int y, Corridor owner)
    {
        this(type,x,y);
        _corridorOwner = owner;
    }
    public Tile(Types type, int x, int y, Room owner)
    {
        this(type,x,y);
        _roomOwner = owner;
    }

    public void setType(Types type)
    {
        _type = type;
        _paint.setColor(textureMap.get(_type));
    }

    public void setNeighbours(ArrayList<Tile> tiles)
    {
        neighbours = tiles;
    }
    public void addNeighbour(Tile neighbour)
    {
        neighbours.add(neighbour);
    }
    public Boolean isAdjacent(Tile otherTile)
    {
        for(Tile t : neighbours)
        {
            if(t == otherTile)
            {
                return true;
            }
        }
        return false;
    }
    public void draw(Canvas canvas)
    {

        //canvas.drawRect(_drawRect.left,_drawRect.top,_drawRect.width(),_drawRect.height(),_paint);
        canvas.drawRect(_drawRect,_paint);
        //Log.w("Tile","Drawing: "+_drawRect);
    }
    public float distanceTo(Tile otherTile)
    {
        return PathFinder.getEuclideanDistance(this, otherTile);
    }
}
